package com.ktjy.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktjy.dto.CourseDTO;
import com.ktjy.pojo.TbCourse;
import com.ktjy.mapper.TbCourseMapper;
import com.ktjy.service.ITbCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;

/**
 * <p>
 * 课程管理 服务实现类
 * </p>
 *
 * @author jjx
 * @since 2025-08-02
 */
@Service
public class TbCourseServiceImpl extends ServiceImpl<TbCourseMapper, TbCourse> implements ITbCourseService {
    @Autowired
    TbCourseMapper tbCourseMapper;

    @Override
    public IPage<TbCourse> findPage(CourseDTO courseDTO, Integer pageNo, Integer pageSize) {
        //构造分页条件
        Page page=new Page(pageNo,pageSize);
        return tbCourseMapper.selectPage(page,courseDTO);
    }

    @Override
    public TbCourse getById(Serializable id) {
        return baseMapper.selectByIdWithValidFields(id);
    }

    @Override
    public boolean saveOrUpdate(TbCourse course) {
        if (course.getId() == null) {
            // 新增：直接调用BaseMapper的insert
            return tbCourseMapper.insertCustom(course) > 0;
        } else {
            // 更新：调用自定义的updateByCustom方法
            return tbCourseMapper.updateByCustom(course) > 0;
        }
    }
    /**
     * 重写删除方法，使用自定义逻辑删除SQL
     */
    @Override
    public boolean removeById(Serializable id) {
        return tbCourseMapper.deleteByIdCustom(id) > 0;
    }

}
